﻿//-----------------------------------------------------------------------
// <copyright file="ServerHelpers.cs" company="None">
// None
// </copyright>
//-----------------------------------------------------------------------

namespace WCFDataServiceFormatExtensions
{
    using System;
    using System.Reflection;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using System.Text;
    using System.Xml;
    using System.IO;
    using System.Web.Configuration;
    using System.Diagnostics;
    using System.Xml.Linq;
    using System.Collections.Generic;
   // using WCFDataService.FormatExtensions;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ServerHelpers
    {
        public static Encoding GetReplyEncoding(HttpResponseMessageProperty response)
        {
            Encoding encoding = Encoding.UTF8;
            string charset = response.Headers["Content-Type"];
            int loc = charset.IndexOf("charset", StringComparison.InvariantCultureIgnoreCase);
            charset = charset.Substring(loc, charset.Length - loc);
            loc = charset.IndexOf("=");
            charset = charset.Substring(charset.IndexOf("=") + 1, charset.Length - loc - 1);
            if (charset.IndexOf(";") > 0)
            {
                loc = charset.IndexOf(";");
                charset = charset.Substring(charset.IndexOf(";") + 1, charset.Length - loc - 1);
            }

            encoding = Encoding.GetEncoding(charset);


            return encoding;
        }

        public static string GetDownloadFolderHttpPath(string hostName, string downloadFolderName)
        {
            string downloadFolderHttpPath = string.Format("{0}/{1}", hostName, downloadFolderName);

            string DownloadFolderVirtualHTTPPath = WebConfigurationManager.AppSettings["DownloadFolderVirtualHTTPPath"];
            if (!string.IsNullOrEmpty(DownloadFolderVirtualHTTPPath))
            {
                downloadFolderHttpPath = DownloadFolderVirtualHTTPPath;
            }
            return downloadFolderHttpPath;
        }

        public static string GetDropLocation(string rootDir, string downloadFolderName)
        {
            string dropLocation = Path.Combine(rootDir, downloadFolderName);
            string DownloadFolderPhysicalPath = WebConfigurationManager.AppSettings["DownloadFolderPhysicalPath"];
            if (!string.IsNullOrEmpty(DownloadFolderPhysicalPath))
            {
                dropLocation = DownloadFolderPhysicalPath;

            }
            return dropLocation;
        }

        public static  string GetHostString(string url)
        {
            // Find third occurance of "/"
            int tempIndex = url.IndexOf("/", 0);
            tempIndex = url.IndexOf("/", tempIndex + 1);
            tempIndex = url.IndexOf("/", tempIndex + 1);
            string hostName = url.Substring(0, tempIndex);
            return hostName;
        }

        public static string GetRootDir()
        {
            string rootDir = Assembly.GetExecutingAssembly().CodeBase;
            rootDir = rootDir.Replace("file:///", string.Empty);
            rootDir = rootDir.Replace('/', '\\');

            FileInfo fn = new FileInfo(rootDir);
            rootDir = fn.DirectoryName;
            if (rootDir.EndsWith("bin", StringComparison.CurrentCultureIgnoreCase))
            {
                rootDir = rootDir.Substring(0, rootDir.IndexOf("bin", StringComparison.CurrentCultureIgnoreCase));
            }


            return rootDir;
        }
    }
}
